// pages/myself/myself.js
const app = getApp();
const db = wx.cloud.database();
const fs = wx.getFileSystemManager();
const defaultAvatarUrl = 'https://mmbiz.qpic.cn/mmbiz/icTdbqWNOwNRna42FI242Lcia07jQodd2FJGIYQfG0LAJGFxM4FbnQP6yfMxBgJ0F3YRqJCJ1aPAK2dQagdusBZg/0';
var avatar = ""
var nickname = ""
var signature = ""
var openid = app.globalData.openid
var emotionPoint = wx.getStorageSync('emotionPoint')
var diary = [];
var exp = [];
var statisticData = [
    {
        "text": "愉悦",
        "emoji": "../../images/laughing.png",
        "num": 0,
        "color": "rgba(255, 166, 56, 1);",
        "ani": ""
    },
    {
        "text": "轻松",
        "emoji": "../../images/smile.png",
        "num": 0,
        "color": "rgba(251, 238, 105, 1);",
        "ani": ""
    },
    {
        "text": "无语",
        "emoji": "../../images/sighing.png",
        "num": 0,
        "color": "rgba(173, 219, 137, 1);",
        "ani": ""
    },
    {
        "text": "发愁",
        "emoji": "../../images/crying.png",
        "num": 0,
        "color": "rgba(154, 168, 203, 1);",
        "ani": ""
    },
    {
        "text": "崩溃",
        "emoji": "../../images/angry.png",
        "num": 0,
        "color": "rgba(164, 156, 150, 1);",
        "ani": ""
    }
];
Page({
    /**
     * 页面的初始数据
     */
    data: {
        defaultAvatarUrl: defaultAvatarUrl,
        avatarUrl : defaultAvatarUrl,
        nickName : nickname,
        signature: signature,
        statisticData: statisticData
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.updateInfo();
    },
    updateInfo(){
        let that = this;

        db.collection("infoDB").where({
            _openid: app.globalData.openid
        }).get({
            success: function(res){
                if ( res.data.length == 0 ) {
                    nickname = "微信用户";
                    avatar = defaultAvatarUrl;
                    signature = "无";
                }
                else {
                    nickname = res.data[0].nickname;
                    avatar = res.data[0].avatar;
                    if ( !avatar ) {
                        avatar = defaultAvatarUrl;
                    }
                    signature = res.data[0].signature;
                }
                that.setData({
                    avatarUrl: avatar,
                    nickName: nickname,
                    nickNameLen: nickname.length,
                    signature: signature
                });
            }
        })

        
    },
    login(){
        let that = this;
        db.collection("infoDB").where({
            _openid: openid
        }).get({
            success: function(res){
                if ( res.data.length == 0 ) {
                    //未登录
                    that.setInfo();
                }
                else {
                    openid = app.globalData.openid;
                    that.setData({
                        avatarUrl: defaultAvatarUrl,
                        nickName: "加载中...",
                        signature: "加载中...",
                        loginSuccessful: true
                    })
                    that.updateInfo();
                }
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {
        if (typeof this.getTabBar === 'function' && this.getTabBar()) {
            this.getTabBar().setData({
                selected: 4
            })
        }
        
    },
    getStatistic(){
        let that = this;
        
        db.collection("diaryDB").where({
            _openid: app.globalData.openid
        }).get({
            success: function(res){
                let data = res.data;
                let diaryDict = {};
                let pics = 0;    
                for ( let i in data ) {
                    let date = String(data[i].year) + String(data[i].month) + String(data[i].day);
                    diaryDict[date] = 1;
                    for ( let j in data[i].cloudPath )
                        pics += 1;
                }
                let diarys = data.length;
                let days = Object.keys(diaryDict).length;
                that.setData({
                    diarys: diarys,
                    days: days,
                    pics: pics
                });
            }
        })
        db.collection("experienceDB").where({
            _openid: app.globalData.openid
        }).get({
            success: function(res){
                exp = res.data;
                let like = 0;
                let favourite = 0;
                for ( let i in exp ) {
                    like += Object.keys(exp[i]["like"]).length;
                    favourite += Object.keys(exp[i]["favourite"]).length;
                }
                that.setData({
                    exp: exp.length,
                    like: like,
                    favourite: favourite
                });
            }
        })
        db.collection("matterDB").get({
            success: function(res){
                that.setData({
                    matters: res.data.length,
                });
            }
        })
    },
    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.updateInfo();
        this.getStatistic();
    },
    gotoDayMatters(){
        wx.navigateTo({
            url: '../DaysMatter/DaysMatter',
          })
    },
    gotoStatistic(){
        wx.navigateTo({
            url: '../Statistic/Statistic',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{avatar:avatar,nickname:nickname,diary:diary});
            }
          })
    },
    gotoSetInfo(){
        wx.navigateTo({
            url: '../setInfo/setInfo',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{avatar:avatar,nickname:nickname,signature:signature});
            }
        })
    },
    gotoMyExp(){
        wx.navigateTo({
            url: '../myExp/myExp',
            success: function(res) {
                res.eventChannel.emit('acceptDataFromOpenerPage',{exp:exp});
            }
        })
    },
    gotoMessage(){
        wx.navigateTo({
            url: '../messages/messages'
        })
    },
    gotoMyFavourite(){
        wx.navigateTo({
            url: '../myFavourite/myFavourite'
        })
        // wx.navigateTo({
        //     url: '../writeArticle/writeArticle'
        // })
    },
    gotoMyPics(){
        wx.navigateTo({
            url: '../myPics/myPics'
        })
    },
    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})